package com.lk.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.dao.IndexMapper;
import com.lk.springboot.dao.UserMapper;
import com.lk.springboot.dao.UserTokenMapper;
import com.lk.springboot.pojo.UserToken;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.vo.Index;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lk
 * @date 2020/1/9 0009 16:41
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserTokenMapper userTokenMapper;
    @Autowired
    IndexMapper indexMapper;

    static final int MAX_MAGIC_TIMES = 3; // 魔法市场交易次数

    @Override
    public Result login(String u_name, String passwd) {
        String u_id = userMapper.login(u_name, passwd);
        Result result = null;
        if (u_id != null) {
            result = new Result(ResultCode.SUCCESS);
            result.setData(u_id);
        } else {
            result = new Result(ResultCode.FAILD_LOGIN);
            result.setData("faild");
        }
        return result;
    }

    @Override
    public void saveToken(UserToken userToken) {
        userTokenMapper.insertToken(userToken);
    }

    @Override
    public String checkID(String token) {
        String uid = userTokenMapper.readToken(token);
        return uid;
    }

    @Override
    public String getIndex(String u_id) {
        indexMapper.signChange(u_id);   // 完成签到，并标记签到进度为 1（已完成）
        int score = indexMapper.getScore(u_id);
        List<Index> list = indexMapper.getSmallPhoos(u_id);
        List<String> sitesList = indexMapper.getAllokSP(u_id);
        JSONObject returnJson = new JSONObject();
        returnJson.put("score", score);
        returnJson.put("sites", list);
        returnJson.put("siteNum", list.size());
        returnJson.put("sitesbest", sitesList);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(returnJson);
        return JSON.toJSONString(result);
    }

    @Override
    public String getmyscore(String u_id) {
        JSONObject returnJson = new JSONObject();
        int score = indexMapper.getScore(u_id);
        returnJson.put("score", score);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(returnJson);
        return JSON.toJSONString(result);
    }

    @Override
    public String getisnew(String u_id) {
        int isnew = userMapper.isNew(u_id);
        Result result = new Result(ResultCode.SUCCESS);
        if (isnew == 0) {//是新用户
            userMapper.setNotNew(u_id);//现在改成不是用户
            result.setData(true);
            return JSON.toJSONString(result);
        } else {//不是新用户
            result.setData(false);
            return JSON.toJSONString(result);
        }
    }

    @Override
    public String getSites(String u_id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("active_sites_num", indexMapper.getUserSitesNum(u_id)); // 目前已激活站点数（指目前该站点至少还有一个碎片）
        jsonObject.put("actived_sites_num", indexMapper.getUserSitesNumED(u_id)); // 截止目前，已激活过站点数（指的是可能 某站点 一个碎片也没有）
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(jsonObject);
        return JSON.toJSONString(result);
    }

    @Override
    public int getMarketTimes(String u_id) {
        int t = indexMapper.getMarketTimes(u_id);
        return MAX_MAGIC_TIMES - t;
    }

    @Override
    public boolean getIsInStation(String u_id) {
        if (userMapper.getUSStatus(u_id) == 0)
            return false;
        else
            return true;
    }

    @Override
    public void alterIsInStation(String u_id, int status) {
        userMapper.updateUSStatus(u_id, status);
    }


}
